package org.dromara.yuanshidian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.yuanshidian.domain.Ab02;
import org.dromara.yuanshidian.domain.vo.ExportAb04Vo;
import org.springframework.stereotype.Service;
import org.dromara.yuanshidian.domain.bo.Ab04Bo;
import org.dromara.yuanshidian.domain.vo.Ab04Vo;
import org.dromara.yuanshidian.domain.Ab04;
import org.dromara.yuanshidian.mapper.Ab04Mapper;
import org.dromara.yuanshidian.service.IAb04Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 原人员账户明细(缴费明细)Service业务层处理
 *
 * @author TzhongTop
 * @date 2025-01-24
 */
@RequiredArgsConstructor
@Service
public class Ab04ServiceImpl implements IAb04Service {

    private final Ab04Mapper baseMapper;

    /**
     * 查询原人员账户明细(缴费明细)
     *
     * @param id 主键
     * @return 原人员账户明细(缴费明细)
     */
    @Override
    public Ab04Vo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询原人员账户明细(缴费明细)列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 原人员账户明细(缴费明细)分页列表
     */
    @Override
    public TableDataInfo<Ab04Vo> queryPageList(Ab04Bo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Ab04> lqw = buildQueryWrapper(bo);
        lqw.orderByAsc(Ab04::getGrmx02);
        Page<Ab04Vo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
    /**
     * 查询符合条件的原人员账户明细(缴费明细)列表
     *
     * @param bo 查询条件
     * @return 原人员账户明细(缴费明细)列表
     */
    @Override
    public List<Ab04Vo> queryList(Ab04Bo bo) {
        LambdaQueryWrapper<Ab04> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(Ab04::getGrmx02);
        return baseMapper.selectVoList(lqw);
    }


    private LambdaQueryWrapper<Ab04> buildQueryWrapper(Ab04Bo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Ab04> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAaa027()), Ab04::getAaa027, bo.getAaa027());
        lqw.eq(StringUtils.isNotBlank(bo.getDw01()), Ab04::getDw01, bo.getDw01());
        lqw.like(StringUtils.isNotBlank(bo.getDw02()), Ab04::getDw02, bo.getDw02());
        lqw.eq(StringUtils.isNotBlank(bo.getGr03()), Ab04::getGr03, bo.getGr03());
        lqw.like(StringUtils.isNotBlank(bo.getGr04()), Ab04::getGr04, bo.getGr04());
        lqw.eq(StringUtils.isNotBlank(bo.getGr02()), Ab04::getGr02, bo.getGr02());
        lqw.eq(StringUtils.isNotBlank(bo.getGrmx01()), Ab04::getGrmx01, bo.getGrmx01());
        lqw.eq(StringUtils.isNotBlank(bo.getGrmx02()), Ab04::getGrmx02, bo.getGrmx02());
        lqw.eq(bo.getGrmx03() != null, Ab04::getGrmx03, bo.getGrmx03());
        lqw.eq(bo.getGr18() != null, Ab04::getGr18, bo.getGr18());
        lqw.eq(bo.getGr19() != null, Ab04::getGr19, bo.getGr19());
        lqw.eq(bo.getGr20() != null, Ab04::getGr20, bo.getGr20());
        lqw.eq(bo.getGrmx04() != null, Ab04::getGrmx04, bo.getGrmx04());
        lqw.eq(bo.getGrmx05() != null, Ab04::getGrmx05, bo.getGrmx05());
        lqw.eq(StringUtils.isNotBlank(bo.getGrmx06()), Ab04::getGrmx06, bo.getGrmx06());
        lqw.eq(bo.getDw14() != null, Ab04::getDw14, bo.getDw14());
        lqw.eq(bo.getCs03() != null, Ab04::getCs03, bo.getCs03());
        lqw.eq(StringUtils.isNotBlank(bo.getDw68()), Ab04::getDw68, bo.getDw68());
        lqw.eq(StringUtils.isNotBlank(bo.getDw24()), Ab04::getDw24, bo.getDw24());
        lqw.eq(bo.getSystime() != null, Ab04::getSystime, bo.getSystime());
        lqw.eq(StringUtils.isNotBlank(bo.getGrjx01()), Ab04::getGrjx01, bo.getGrjx01());
        lqw.eq(StringUtils.isNotBlank(bo.getTz01()), Ab04::getTz01, bo.getTz01());
        lqw.eq(StringUtils.isNotBlank(bo.getDh()), Ab04::getDh, bo.getDh());
        lqw.eq(StringUtils.isNotBlank(bo.getDw91()), Ab04::getDw91, bo.getDw91());
        lqw.eq(bo.getAaz223() != null, Ab04::getAaz223, bo.getAaz223());
        lqw.eq(bo.getStatus() != null, Ab04::getStatus, bo.getStatus());
        lqw.eq(bo.getDataFlag() != null, Ab04::getDataFlag, bo.getDataFlag());
        lqw.eq(bo.getDelStatus() != null, Ab04::getDelStatus, bo.getDelStatus());
        lqw.eq(bo.getJfStarttime() != null, Ab04::getJfStarttime, bo.getJfStarttime());
        lqw.eq(bo.getJfEndtime() != null, Ab04::getJfEndtime, bo.getJfEndtime());
        lqw.eq(StringUtils.isNotBlank(bo.getFileurl()), Ab04::getFileurl, bo.getFileurl());
        lqw.eq(StringUtils.isNotBlank(bo.getGr01()), Ab04::getGr01, bo.getGr01());
        lqw.eq(bo.getJsStartmonth() != null, Ab04::getJsStartmonth, bo.getJsStartmonth());
        lqw.eq(bo.getJsEndmonth() != null, Ab04::getJsEndmonth, bo.getJsEndmonth());
        lqw.eq(bo.getJfPaymentmonths() != null, Ab04::getJfPaymentmonths, bo.getJfPaymentmonths());
        lqw.eq(bo.getGrjfbx() != null, Ab04::getGrjfbx, bo.getGrjfbx());
        lqw.eq(bo.getGrzhbx() != null, Ab04::getGrzhbx, bo.getGrzhbx());
        lqw.eq(bo.getRefStatus() != null, Ab04::getRefStatus, bo.getRefStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getJfssfbz()), Ab04::getJfssfbz, bo.getJfssfbz());
        return lqw;
    }

    /**
     * 新增原人员账户明细(缴费明细)
     *
     * @param bo 原人员账户明细(缴费明细)
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Ab04Bo bo) {
        Ab04 add = MapstructUtils.convert(bo, Ab04.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改原人员账户明细(缴费明细)
     *
     * @param bo 原人员账户明细(缴费明细)
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Ab04Bo bo) {
        Ab04 update = MapstructUtils.convert(bo, Ab04.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Ab04 entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除原人员账户明细(缴费明细)信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 原人员账户明细(缴费明细)退费专用导出列表
     * @param ab04 账户明细(缴费明细)
     * @return 账户明细(缴费明细)
     */
    @Override
    public List<ExportAb04Vo> exportAb04(Ab04Bo ab04, String[] splitID) {
        return baseMapper.exportAb04(ab04,splitID);
    }

    /**
     * 批量修改已经退费的状态
     */
    @Override
    public Boolean updateAb04(String[] splitID) {
        return baseMapper.updateAb04(splitID)>0;
    }

}
